package com.example.myapplication

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import java.text.SimpleDateFormat
import java.util.*

class PatientAdapter(
    private var patients: List<Patient>,
    private val onItemClick: (Patient) -> Unit,
    private val onDeleteClick: (Patient) -> Unit,
    private val onEditClick: (Patient) -> Unit
) : RecyclerView.Adapter<PatientAdapter.PatientViewHolder>() {

    class PatientViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textName: TextView = itemView.findViewById(R.id.textName)
        val textExaminationDate: TextView = itemView.findViewById(R.id.textExaminationDate)
        val textReporter: TextView = itemView.findViewById(R.id.textReporter)
        val textExaminationName: TextView = itemView.findViewById(R.id.textExaminationName)
        val textDepartment: TextView = itemView.findViewById(R.id.textDepartment)
        val textReportTime: TextView = itemView.findViewById(R.id.textReportTime)
        val textUserType: TextView = itemView.findViewById(R.id.textUserType)
        val buttonDelete: Button = itemView.findViewById(R.id.buttonDelete)
        val buttonEdit: Button = itemView.findViewById(R.id.buttonEdit)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PatientViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_patient, parent, false)
        return PatientViewHolder(view)
    }

    override fun onBindViewHolder(holder: PatientViewHolder, position: Int) {
        val patient = patients[position]
        val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
        val timeFormat = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault())
        
        holder.textName.text = "患者: ${patient.name}"
        holder.textExaminationDate.text = "检查日期: ${dateFormat.format(patient.examinationDate)}"
        holder.textReporter.text = "汇报人: ${patient.reporter}"
        holder.textExaminationName.text = "检查项目: ${patient.examinationName}"
        holder.textDepartment.text = "科室: ${patient.department}"
        holder.textReportTime.text = "上报时间: ${timeFormat.format(patient.reportTime)}"
        
        // 显示用户类型
        val userTypeText = when (patient.reportedByType) {
            UserType.SUPER_SUPER_ADMIN -> "大总管理员"
            UserType.SUPER_ADMIN -> "总管理员"
            UserType.DEPARTMENT_ADMIN -> "科室管理员"
            UserType.USER -> "普通用户"
            else -> "未知角色"
        }
        holder.textUserType.text = "上报用户类型: $userTypeText"
        
        // 设置点击事件
        holder.itemView.setOnClickListener {
            onItemClick(patient)
        }
        
        // 设置删除按钮
        holder.buttonDelete.setOnClickListener {
            onDeleteClick(patient)
        }
        holder.buttonEdit.setOnClickListener {
            onEditClick(patient)
        }
    }

    override fun getItemCount(): Int = patients.size

    fun updatePatients(newPatients: List<Patient>) {
        patients = newPatients
        notifyDataSetChanged()
    }
} 